var common = require("../../utils/common.js");
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    banner: '',
    name: '',
    tel: '',
    hasWaterLogo: false
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log("order的onLoad")
    wx.setNavigationBarTitle({
      title: '看房预约'
    });
    if (app.globalData.xcxtype.hasLogo == '1') {
      //显示水印
      this.setData({
        hasWaterLogo: true
      })
    } else {
      this.setData({
        hasWaterLogo: false
      })
    };
    var that = this;
    common.ajaxPost(
      'portal/wlpBuilding/queryBuilding.action', {
        id: app.globalData.lpid
      },
      function (res) {
        if (res && res.length > 0) {
          console.log(res);
          var data = res[0];
          var banner = data.banners.split(";")[0]
          var coverPic = data.coverPic;
          var tips = data.houseLable.split("|");
          var position = common.bd09togcj02(data.longitude, data.latitude);
          that.setData({
            banner: banner,
            coverPic: coverPic
          })
        }
      }
    )
  },
  rule: function (e) {
    wx.navigateTo({
      url: '/pages/rule/rule'
    })
  },
  name: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  tel: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  submit: function () {
    var that=this;
    var name = that.data.name;
    var tel = that.data.tel;
    if (!name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return false;
    }
    if (!tel) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return false;
    }
    var reg = /^1[0-9]{10}$/;
    if (tel.length != 11 || !reg.test(tel)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none'
      })
      return false;
    }

    common.ajaxPost(
      'portal/appointment/save.action',
      {
        name: name,
        phone: tel,
        bid: app.globalData.bid
      },
      function (res) {
        if (res.result) {
          wx.showToast({
            title: '提交预约成功',
            duration:3000,
            success:function(){
              // setTimeout(that.goBack,1000)
              that.goBack();
            }
          })

        }
      }
    )
  },
  onShow: function () {
    //检查是否有phone存在
    var that=this;
    wx.getStorage({
      key: 'phone',
      success: function(res) {
        if(res.data && res.data!=""){
           that.setData({
             tel: res.data
           })
        }
      },
    })
  },
  goBack:function(){
    console.log("退出了")
    setTimeout(function () {
      wx.switchTab({
        url: '/pages/shouye/shouye'
      }) }, 1500)
  },
  onShareAppMessage: function (res) {
    var xcxids;
    if (app.globalData.xcxshareid) {
      xcxids = app.globalData.xcxshareid
    } else {
      xcxids = app.globalData.queryWlpXcxByParams[0].id
    }
    return {
      title: '看房预约',
      path: '/pages/index/index?type=order&bid=' + app.globalData.bid + "&isIndex=1" + "&xcxid=" + xcxids+ "&lpid=" + app.globalData.lpid,
      success: function (res) {
        // 转发成功
        console.log('/pages/index/index?type=order&bid=' + app.globalData.bid + "&isIndex=1" + "&xcxid=" + xcxids)
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onUnload:function(){
    app.globalData.isShareBack=true
    console.log("order退出了")
  }
})
